<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<style type="text/css">
			body{
				position: absolute;
				left:0;
				top: 0;
			}
			.loadimg {
				position: absolute; 
				left: 0;
				top: 0;
				background: url(img/1-loading2.png);
				background-size: 100% 100%;
				height: 100vh;
				width: 100vw;
			}
			
			.update_div{
				margin: auto auto;
				margin-top: 100px;
				background: #FFFFFF;
				border-radius: 5px;
				height: 350px;
				width: 250px;
				text-align: left;
				display: none;
			}
			
			
			.update_title img{
				width: 100%;
			}
			.update_title{
				width: 100%;
				text-align: right;
				font-size: 14px;
			}
			.update_info{
				border: 0;
				overflow:scroll;
				height: 150px;
				font-size: 14px;
				padding: 0;
				padding-left: 10px;
			}
			.update_button_cancel{
				text-align: center;
				width: 40%;
				line-height: 30px;
				border: 1px #D1D1D1 solid;
				border-radius: 5px;
				background: #D1D1D1;
				color: #FFFFFF;
				margin-top: 18px;
			}
			/*.update_button_cancel:active{
				opacity: 0.5;
			}*/
			.update_button_update{
				text-align: center;
				width: 40%;
				line-height: 30px;
				border: 1px #F28103 solid;
				border-radius: 5px;
				margin-top: 18px;
				color: #FFFFFF;
				background: #F28103;
			}
			/*.update_button_update:active{
				opacity: 0.5;
			}*/
			.progress img{
				width: 30%;
			}
			.progress{
				text-align: center;
				font-size: 14px;
			}
			.progress_center{
				position: relative;
			}
			.progress_center span{
				
				position: absolute;
				top: -10px;
				left: 20%;
				width: 100%;
			}
			#current_version{ 
				float: left;
				margin-left: 10px;
				color: #A4A4A4;
			}
			#new_version{
				float: right;
				margin-right: 10px;
				color: #F08A13;
			}
			/*loading动画*/
			#loading{
				display: none;
				width: 100vw;
				height: 100vh;
				position: absolute;	
				background: rgba(0,0,0,0);
				left: 0;
				top: 0;
				z-index: 9999;
			}
			.spinner {
			  width: 20px;
			  height: 20px;
			  position: absolute;
			  top: 50%;
			  left: 50%;
			  margin-left: -10px;
			  margin-top: -10px;
			}
			 
			.container1 > div, .container2 > div, .container3 > div {
			  width: 6px;
			  height: 6px;
			  background-color: #167ECD;
			 
			  border-radius: 100%;
			  position: absolute;
			  -webkit-animation: bouncedelay 1.2s infinite ease-in-out;
			  animation: bouncedelay 1.2s infinite ease-in-out;
			  -webkit-animation-fill-mode: both;
			  animation-fill-mode: both;
			}
			 
			.spinner .spinner-container {
			  position: absolute;
			  width: 100%;
			  height: 100%;
			}
			 
			.container2 {
			  -webkit-transform: rotateZ(45deg);
			  transform: rotateZ(45deg);
			}
			 
			.container3 {
			  -webkit-transform: rotateZ(90deg);
			  transform: rotateZ(90deg);
			}
			 
			.circle1 { top: 0; left: 0; }
			.circle2 { top: 0; right: 0; }
			.circle3 { right: 0; bottom: 0; }
			.circle4 { left: 0; bottom: 0; }
			 
			.container2 .circle1 {
			  -webkit-animation-delay: -1.1s;
			  animation-delay: -1.1s;
			}
			 
			.container3 .circle1 {
			  -webkit-animation-delay: -1.0s;
			  animation-delay: -1.0s;
			}
			 
			.container1 .circle2 {
			  -webkit-animation-delay: -0.9s;
			  animation-delay: -0.9s;
			}
			 
			.container2 .circle2 {
			  -webkit-animation-delay: -0.8s;
			  animation-delay: -0.8s;
			}
			 
			.container3 .circle2 {
			  -webkit-animation-delay: -0.7s;
			  animation-delay: -0.7s;
			}
			 
			.container1 .circle3 {
			  -webkit-animation-delay: -0.6s;
			  animation-delay: -0.6s;
			}
			 
			.container2 .circle3 {
			  -webkit-animation-delay: -0.5s;
			  animation-delay: -0.5s;
			}
			 
			.container3 .circle3 {
			  -webkit-animation-delay: -0.4s;
			  animation-delay: -0.4s;
			}
			 
			.container1 .circle4 {
			  -webkit-animation-delay: -0.3s;
			  animation-delay: -0.3s;
			}
			 
			.container2 .circle4 {
			  -webkit-animation-delay: -0.2s;
			  animation-delay: -0.2s;
			}
			 
			.container3 .circle4 {
			  -webkit-animation-delay: -0.1s;
			  animation-delay: -0.1s;
			}
			 
			@-webkit-keyframes bouncedelay {
			  0%, 80%, 100% { -webkit-transform: scale(0.0) }
			  40% { -webkit-transform: scale(1.0) }
			}
			 
			@keyframes bouncedelay {
			  0%, 80%, 100% {
			    transform: scale(0.0);
			    -webkit-transform: scale(0.0);
			  } 40% {
			    transform: scale(1.0);
			    -webkit-transform: scale(1.0);
			  }
			}
			/*load动画结束*/
			
			#button_div{
				height: 48px;
				width: 250px;
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				align-items: center;
			}
			
			.button_div_{
				height: 48px;
				width: 250px;
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				align-items: center;
				
			}
			
			.test{
				display: inline-block;
				background: url(img/banben-3@2x.png) no-repeat bottom center;
				background-size: 100% 5px;
				width: 20% ;
				margin-bottom: 3px;
			}
		</style>
	</head>

	<body>
		<div class="loadimg">
			<div id="update_div" class="update_div">
				<div class="update_title">
					<img src="img/banben-1@2x.png"/>
					<span id="current_version2">2.0.0</span>
				</div>
				<div id="update_info" readonly="readonly" class="update_info">
					<p>更新内容</p> 
				</div>
				<div class="progress">
					<div>
						
						<span id="current_version">2.0.0</span>
						<span class="progress_center">
							<span id="progressNumber">0%</span>
							<img src="img/banben-3@2x.png"/>
						</span>
						<span id="new_version">2.0.1</span>
					</div>
					<div id="mui-progressbar" class="mui-progressbar" style="height: 4px;">
						<span></span>
					</div>
				</div>
				<div id="button_div">
					<div id="update_button_cancel" class="update_button_cancel">
						以后再说
					</div>
					<div id="update_button_update" class="update_button_update">
						立即升级
					</div>
				</div>
			</div>
		</div>
		
		<!--loading动画-->
		
		<div id="loading">
			<div  class="spinner">
			  <div class="spinner-container container1">
			    <div class="circle1"></div>
			    <div class="circle2"></div>
			    <div class="circle3"></div>
			    <div class="circle4"></div>
			  </div>
			  <div class="spinner-container container2">
			    <div class="circle1"></div>
			    <div class="circle2"></div>
			    <div class="circle3"></div>
			    <div class="circle4"></div>
			  </div>
			  <div class="spinner-container container3">
			    <div class="circle1"></div>
			    <div class="circle2"></div>
			    <div class="circle3"></div>
			    <div class="circle4"></div>
			  </div>
			</div>
		</div>
	<script src="../../js/mui.min.js"></script>
	<script type="text/javascript" src="../../js/jquery-1.7.2.min.js" ></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../js/mkeyplus.base.js" ></script>
	<script type="text/javascript">
		mui.init();
		var wgtVer=null;//应用当前版本

		mui.plusReady(function() {
//			if(mui.os.ios){
//				plus.ui.setFullscreen(true);
//			}
			// 获取设备信息
			// plus.device.getInfo({
			// 	success:function(e){
			// 		console.log('getDeviceInfo success: '+JSON.stringify(e));
			// 		localStorage.setItem("uuid", e.uuid);
			// 	},
			// 	fail:function(e){
			// 		console.log('getDeviceInfo failed: '+JSON.stringify(e));
			// 	}
			// });
			
			//获取版本更新
			mui.baseApp.checkUpdate(updateSuccess,chickUpdateError);
			// mui.baseApp.openWindow("../login/login.html");
			//忽略版本
			document.getElementById('update_button_cancel').addEventListener('tap',function(){
				//保存当前忽略的版本号
				localStorage.setItem("update_cancel_version",new_version);
				cancelupdate();
			});
			//升级版本
			document.getElementById('update_button_update').addEventListener('tap',function(){
			    console.log("update"+down_url);
			    //判断是否是完整URL
			    if(down_url.indexOf("http://") <0){//相对url
			    	down_url = down_url.replace(/\\/g,'\/');
			    	down_url = mui.baseApp.serverUrl+down_url;
			    	console.log("dowm_url="+down_url);
			    }
				//更新

				//判断    wgt/apk直接下载
				if(down_url.indexOf("apk") >0 || down_url.indexOf("wgt") >0
					|| down_url.indexOf("wgtu") > 0){
					download(down_url);	
				}else{
					plus.runtime.openURL(down_url);//打开第三方连接
				}
			});
			
		})
		
		function updateSuccess(data){ 
			console.log("updateSuccess:"+JSON.stringify(data));
			console.log(localStorage.getItem("update_cancel_version"));
			console.log(localStorage.getItem("currVersion"));
			if(data && data.version){
				
				if( data.version > localStorage.getItem("currVersion")){
					//
					var update_cancel_version = localStorage.getItem("update_cancel_version");
					//判断是否存忽略版本
					//如果存在，切忽略版等于当前版本则打开登录界面
					if(update_cancel_version != null && update_cancel_version == data.version){
						cancelupdate();
					}else{
						cancelupdate();
						/*
						new_version = data.version;
						down_url = data.downloadurl;
						showAlert(data);
						*/
					}
					
				}else{
					//不更新  打开新页面
					cancelupdate();
				}
				
			}else{
				//不更新  打开新页面
				cancelupdate();
			}
		}
		
		
		
		// 下载wgt文件
		function download(wgtUrl){
		    console.log("升级包地址："+wgtUrl);
		  	var dtask = plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
		        if ( status == 200 ) { 
		            console.log("下载更新成功："+d.filename);
		            installWgt(d.filename); // 安装wgt包
		        } else {
		            console.log("下载更新失败！");
		            alert("下载更新失败！");
		            cancelupdate();
					
		        }
		    });
		    //监听下载状态
		    dtask.addEventListener( "statechanged", function(task,status){
		    	console.log(""+dtask);
		    	console.log(""+JSON.stringify(task));
		    	if(!dtask){return;}
		    	switch(task.state) {
		    		case 1: // 开始
		    			console.log( "开始下载..." );
		    		break;
		    		case 2: // 已连接到服务器
		    			console.log( "链接到服务器..." );
		    		break;
		    		case 3:	// 已接收到数据
		    			var jindu = percentNum(task.downloadedSize,task.totalSize);
		    			mui("#mui-progressbar").progressbar().setProgress(jindu);
		    			document.getElementById("progressNumber").innerText=""+jindu+"%";
		    		break;
		    		case 4:	// 下载完成
		    			document.getElementById("progressNumber").innerText = "下载完成！";
		    			setTimeout(function(){
		    				document.getElementById("progressNumber").innerText = "正在安装！";
		    			},300)
		    			console.log( task.totalSize );
		    		break;
		    	}
		    } );
		    dtask.start();
		}
		
		
		// 更新应用资源
		function installWgt(path){
		    console.log(path);
		    plus.runtime.install(path,{force: true},function(){
		        console.log("安装更新文件成功！点击确定重启应用以完成更新！");
		    	mui.toast("安装更新文件成功！将重启应用！");
		        plus.runtime.restart();
		    },function(e){
		        console.log("安装更新文件失败["+e.code+"]："+e.message);
		        alert("安装更新文件失败["+e.code+"]："+e.message);
		    });
		}
		
		
		//显示更新
		function showAlert(data){
			document.getElementById("update_div").style.display = "block";
			//更新内容
//			var str = data.verdesc.replace(/<\/br>/g, '_@').replace(/\r/g, '_#');
			document.getElementById("update_info").innerHTML=data.verdesc;
			document.getElementById("new_version").innerText=data.version;
			document.getElementById("current_version2").innerText="最新版本："+data.version;
			document.getElementById("current_version").innerText=localStorage.getItem("currVersion");
			//判断是否强制更新  0可选更新   1强制更新
			if(data.vforce == "1"){
				document.getElementById('update_button_cancel').style.display = "none";
			}
		}
		
		//计算百分比
	    function percentNum(num, num2) { 
		     return (Math.round(num / num2 * 100)); //位百分比
		}
		
		
		
		//自动登录
		function autologin(username,userpwd) {
			$("#loading").css("display","block");//loading显示			
			mui.baseApp.httpRequst('/api/v2/users/login',{
				username: '' + username,
				password: '' + userpwd
			},function(data){
				plus.nativeUI.closeWaiting();
				if(data.status == "200") {
					console.log('登录成功=====' + JSON.stringify(data.data));
					var data = data.data;
					console.log('userid======' + data.id);
					localStorage.setItem('userid', data.id);
					localStorage.setItem('unit', data.unit);
					localStorage.setItem('username', data.username);
					localStorage.setItem("windName",JSON.stringify(data.windname));
					console.log('windName++++'+localStorage.getItem('windName'));
					mui.openWindow('../../index1.html');
				} else if(data.status == "400") {
					mui.toast('密码错误,请重新输入!');
					mui.baseApp.openWindow("../login/login.html");
				} else if(data.status == "409") {
					mui.toast('您输入的用户名不存在!');
					mui.baseApp.openWindow("../login/login.html");
				}
//					loginnum = 0;	
			},function(){
				$("#loading").css("display","none");//loading隐藏 
				mui.baseApp.openWindow("../login/login.html");
			}
			);
			
		}
		//登录成功，跳转首页
		function loginsuccess() {
			$("#loading").css("display","none");//loading隐藏 
//			plus.ui.toast("登陆成功");
			if(plus.webview.getWebviewById("../../index1.html")){
				plus.webview.getWebviewById("../../index1.html").close();
			}
			//../home/home.html
			mui.baseApp.openWindow("../../index1.html");
			setTimeout(function(){
				mui.currentWebview.close();
			},2000);
		}
		
		//检查更新失败调用
		function chickUpdateError(data){
			$("#loading").css("display","none");//loading隐藏 //关闭等待
			cancelupdate();//自动登录或者进入主页
		}
		
		//登录或者跳转登录
		function cancelupdate(){
			var username = localStorage.getItem("username"); //取出用户名缓存
			var userpwd = localStorage.getItem("pwd"); //取出用户名缓存
			console.log('mima++++'+userpwd);
			if(username != null && userpwd != null&& userpwd != '') { //如果账号密码不为空，尝试登陆
				autologin(username,userpwd); //登录方法
			} /*else if(localStorage.getItem("firstopen") == undefined) {
			//第一次进入应用，跳转到滑动介绍
				mui.baseApp.openWindow("guide.html");
			} */else { //自动登录失败，进入登录页
//				mui.baseApp.openWindow("../login/login.html");
				mui.openWindow({
					//url: '../neom/formal/registerWNext.html',
					url: '../neom/formal/login_ext.html',
					//url:'../neom/test/video.html',
					id: 'login_ext',
					extras: {
						auto:'yes'
					}
				})
			}
		}
		
		
		</script>
	</body>
</html>